package com.example.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.example.dao.impl.PeopleDaoImpl;
import com.example.dao.impl.UserDaoImpl;
import com.example.dao.impl.UploadPicDaoImpl;
import com.example.dao2.PeopleDao;
import com.example.dao2.UserDao;
import com.example.dao2.VehicleDao;
import com.example.dao2.UploadPicDao;
import com.example.entity.People;
import com.example.entity.UploadFile;
import com.example.entity.User;
import com.example.entity.VehicleFaultInfo;
import com.example.utils.UuidUtil;
import com.google.gson.Gson;

public class UploadFileServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("enter in uploadPicServlet ");
		//System.out.println("request is "+request.getHeaderNames().toString());
		response.setContentType("text/html");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		UploadPicDao upd = new UploadPicDaoImpl();

		UploadFile uf = new UploadFile();
		String id = new UuidUtil().getUuid();
		String filePath = "";
		uf.setId(id);
		if (isMultipart) {
			String realpath = request.getSession().getServletContext()
					.getRealPath("/uploadFiles");
			System.out.println(realpath);
			File dir = new File(realpath);
			if (!dir.exists())
				dir.mkdirs();
			FileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			upload.setHeaderEncoding("UTF-8");
			try {
				List<FileItem> items = upload.parseRequest(request);
				for (FileItem item : items) {
					// 如果请求是文本

					if (item.isFormField()) {
						String fieldName = item.getFieldName();
						if (fieldName.toLowerCase().equals("uploadtime")) {
						
							uf.setUploadTime(item.getString());

						}
						if (fieldName.toLowerCase().equals("filedesc")) {
							String message = new String(item.getString().getBytes("UTF-8"), "utf-8");
							System.out.println("message is " + message);
							uf.setFileDesc(item.getString());
						}
						String name1 = item.getFieldName();// 得到请求参数的名称
						String value = item.getString("UTF-8");// 得到参数值
						System.out.println(name1 + "=" + value);
					} else {
						filePath = realpath
								+ "/"
								+ System.currentTimeMillis()
								+ item.getName().substring(
										item.getName().lastIndexOf("."));
						System.out.println("filePath" + filePath);
						item.write(new File(filePath));
					}
				}
				uf.setFilePath(filePath);
				upd.save(uf);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
		//	doGet(request, response);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	// 初始化方法
	public void init() throws ServletException {

	}

	// 构造方法
	public UploadFileServlet() {
		super();
	}

	// 销毁方法
	public void destory() {
		super.destroy();
	}

	public String beanToJson(People people) {

		Gson gson = new Gson();
		System.out.println("----------简单对象之间的转化-------------");
		// 简单的bean转为json
		String s1 = gson.toJson(people);
		// System.out.println("简单Bean转化为Json===" + s1);
		return s1;

	}
}
